package com.googlecode.intellimars.derby;

import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.ApplicationComponent;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.IconLoader;
import org.apache.derby.drda.NetworkServerControl;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.EnumMap;

public class DerbyPlugin implements ApplicationComponent {
    private static final Logger LOGGER = Logger.getInstance("Derby");

    private static EnumMap<STATE, Icon> ICONS = new EnumMap<STATE, Icon>(STATE.class) {
        {
            put(STATE.OFF, IconLoader.getIcon("/com/googlecode/intellimars/derby/derby-off.gif"));
            put(STATE.ON, IconLoader.getIcon("/com/googlecode/intellimars/derby/derby-on.gif"));
            put(STATE.ERROR, IconLoader.getIcon("/com/googlecode/intellimars/derby/derby-error.gif"));
        }
    };

    private STATE state = STATE.OFF;
    private NetworkServerControl serverControl;


    public void toggleState() {
        try {
            if (serverControl == null) {
                serverControl = new NetworkServerControl();
            }
            if (state == STATE.ON) {
//                serverControl.shutdown();
                shutdown();
                state = STATE.OFF;
            }
            else {
//                serverControl.start(new PrintWriter(System.out));
                start();
                state = STATE.ON;
            }
        }
        catch (Exception e1) {
            LOGGER.error(e1);
            state = STATE.ERROR;
        }

    }

    public Icon getStateIcon() {
        return ICONS.get(state);
    }

    public String getState() {
        return state.toString();
    }

    public static DerbyPlugin getInstance() {
        return ApplicationManager.getApplication().getComponent(DerbyPlugin.class);
    }


    public void initComponent() {
        // TODO: insert component initialization logic here
    }

    public void disposeComponent() {
        try {
            if (state == STATE.ON && serverControl != null) shutdown();

//                serverControl.shutdown();
        }
        catch (Exception e) {
            LOGGER.error(e);
        }
    }

    @NotNull
    public String getComponentName() {
        return "DerbyPlugin";
    }


    private void start() throws Exception {
        GeneralCommandLine cmd = new GeneralCommandLine();
        cmd.setExePath("c:/temp/javadb/bin/startNetworkServer.bat");
        Process process = cmd.createProcess();
        LOGGER.info("derby start");
    }

    private void shutdown() throws Exception {
        GeneralCommandLine cmd = new GeneralCommandLine();
        cmd.setExePath("c:/temp/javadb/bin/stopNetworkServer.bat");
        Process process = cmd.createProcess();
        LOGGER.info("derby stop");
    }

}
